﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockImport.dataimport.otc.dailyindex;
using Utilities;
using StockImport.TaskLog;
using StockCommon;

namespace StockImport.dataimport
{
    public abstract class HttpContentFetcher_Daily : HttpContentFetcher
    {

        public abstract UpdateResult update(DateTime date, bool force = false);
        
        public void updateToDate(DateTime date, int maxHitCount = 200)
        {
            date = DateUtils.getDateOnly(date);
            Guid id = ObjectExecServices.getServicesId("sitefetcher", getTaskName(), getVersion());
            //有一個向前更新的
            DateTime targetDate = date;

            int hitCount = 0;
            while (true)
            {
                if (suitableForDate(targetDate) == false)
                    break;

                if (hitCount > maxHitCount)
                    break;

                ObjectExecLog exelog = ObjectExecServices.getExecLog(id, "get", targetDate);
                if (islogComplete(exelog))
                {
                    hitCount++;
                }
                else
                {
                    UpdateResult result = update(targetDate, false);
                    if (result.getUpdateResult())
                    {
                        
                        exelog.status = (int)LogStatus.sUpdated;
                        if (result.getContentResult())
                        {
                            exelog.result = (int)LogResult.rComplete;
                        }
                        else
                        {
                            exelog.result = (int)LogResult.rNoData;
                        }
                    }
                    else
                    {

                    }
                    ObjectExecServices.updateExecLog(exelog);
                }
                targetDate = targetDate.AddDays(-1);
                hitCount++;
            }
        }

    }
}
